#include "CanTask.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#include "CanBLL.h"
#include "CanProtolProc.h"
#include "main.h"

osThreadId canTaskHandle;
void StartCanTask(void const * argument);

osMessageQId g_CanMsgQueueHandle;

void InitCanTask(void)
{
	CanBLL_Init();
	
	osThreadDef(canTask, StartCanTask, osPriorityNormal, 0, 4096);
  canTaskHandle = osThreadCreate(osThread(canTask), NULL);
	
	osMessageQDef(g_CanMsgQueue, 8, uint32_t);
  g_CanMsgQueueHandle = osMessageCreate(osMessageQ(g_CanMsgQueue), NULL);
}


void StartCanTask(void const * argument)
{
	osEvent evt;
	uint32_t u32DataLength = 0;
	CanRxFrame canRxFrame;
	struct fifo* canFifo = GetCanFifoAddr();

	while(1)
	{
		evt = osMessageGet(g_CanMsgQueueHandle, osWaitForever);
		if(evt.status == osEventMessage)
		{
			u32DataLength = (uint32_t)evt.value.p;
			fifo_out(canFifo, (void *)&canRxFrame, u32DataLength);
//			printf("CanID = %0x\n\r", canRxFrame.rxHeader.StdId);
			
			ProcCanData(canRxFrame);

#if DEBUG			
			for(uint8_t i = 0; i < canRxFrame.rxHeader.DLC; i ++)
			{
				printf("%0x ", canRxFrame.rxData[i]);
			}
			printf("\n\rLength = %d\n\r", canRxFrame.rxHeader.DLC);
#endif
		}
	}
}


void StartCanProcTask(void const *argument)
{


}

